<template>
    <el-row class="model">
        <el-row type="flex" style="height: 40px;margin-bottom: 14px;position: relative">
            <el-button type="primary" size="medium" @click="addSmt" style="width: 130px;position: absolute;right: 0;">新增设备</el-button>
        </el-row>
        <el-table
                :data="modelData"
                size="mini"
                @current-change="userChge"
                height="730"
                max-height="730"
                :header-cell-style="{background:'#e5ecf1',fontWeight: 'bold',textAlign: 'center'}"
                highlight-current-row
                border
                style="font-size: 16px;"
        >
            <el-table-column prop="name" label="设备名称" min-width="180"></el-table-column>
            <el-table-column prop="code" label="设备编号" min-width="180"></el-table-column>
            <el-table-column prop="yardName" label="场地名称" min-width="180"></el-table-column>
            <el-table-column prop="firmCode" label="厂商编码" min-width="180"></el-table-column>
            <el-table-column prop="modnName" label="设备型号" min-width="180"></el-table-column>
            <el-table-column prop="tyidName" label="设备类型" min-width="180"></el-table-column>
            <el-table-column width="300" fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="editSmt( scope.row )"
                               icon="el-icon-edit">编辑
                    </el-button>
                    <el-button type="danger" size="mini" @click="deleteSmt( scope.row )"
                               icon="el-icon-delete">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-row type="flex" justify="center" class="pagination">
            <el-pagination
                    @size-change="userSize"
                    @current-change="pgChge"
                    :current-page="modelPage.currentPage"
                    :page-sizes="[10, 20, 30]"
                    :page-size="modelPage.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="modelPage.totalElements"
            ></el-pagination>
        </el-row>
        <transition name="el-fade-in">
            <floatingBox v-show="editBoxShow">
                <addSmt v-if="addShow" slot="edit" @editState="editState"></addSmt>
                <editSmt v-if="editShow" slot="edit" :smtUuid="uuid" @editState="editState"></editSmt>
            </floatingBox>
        </transition>
    </el-row>
</template>

<script>
    import {getDataForSP, writePageInfo} from "../../../utils/webUtils.js";
    import floatingBox from "../../public/floatingBox.vue";
    import editSmt from "./editSmt.vue";
    import addSmt from "./addSmt.vue";

    export default {
        name: "model",
        components: {floatingBox, addSmt, editSmt},
        data() {
            return {
                editBoxShow: false,
                addShow: false,
                editShow: false,
                userSchOn: '',
                userEdit: false,
                uuid: '',
                modelData: [],
                modelPage: {
                    pageSize: 10,//每页显示条目个数
                    currentPage: 1,//当前页数
                    totalElements: 0,
                    searchData: {
                        srhData: ''
                    }
                }
            };
        },
        mounted() {
            this.querySmt();
        },
        methods: {
            addSmt() {
                this.editBoxShow = true;
                this.addShow = true;
                this.editShow = false;
            },
            editState(msg) {
                this.editBoxShow = msg;
                this.editShow = msg;
                this.addShow = msg;
                this.userEdit = msg;
                this.querySmt();
            },
            editSmt(val) {
                this.uuid = val.uuid;
                this.editBoxShow = true;
                this.editShow = true;
                this.userEdit = true;
            },
            deleteSmt(val) {
                this.$confirm('确认删除 ' + val.name + " ?", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.axios.request({
                        method: "delete",
                        url: "./smt/kdi/dev/" + val.uuid,
                        data: {}
                    }).then(() => {
                        this.$message({
                            message: "删除成功!",
                            type: 'success'
                        });
                        this.querySmt();
                    }).catch(error => {
                        console.log(error);
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            userChge(val) {
                if (this.userEdit) {
                    this.uuid = val.uuid;
                }
            },
            //获取所有数据
            querySmt() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/dev/search/findAll",
                    params: getDataForSP(this.modelPage)
                }).then(response => {
                    this.modelData = response.data.content;
                    writePageInfo(this.modelPage, response.data);
                }).catch(error => {
                    console.log(error);
                });
            },
            //列表分页
            userSize: function (pageSize) {
                this.modelPage.pageSize = pageSize;
                if (this.modelPage.currentPage === 1) {
                    if (this.userSchOn !== "") {
                        // this.userSearch();
                    } else {
                        this.querySmt();
                    }
                }
                this.modelPage.currentPage = 1;
            },
            pgChge(currentPage) {
                this.modelPage.currentPage = currentPage;
                this.querySmt();
            }
        }
    };
</script>

<style scoped>
    .model {
        width: calc(100% - 100px);
    }


</style>